// pages/classify/classify.js
var url = wx.getStorageSync('url')
var shopid = wx.getStorageSync('shopid')
var time_ss = wx.getStorageSync('time_ss') 
var t
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width:'40%',
    jiage:'288',
    countDownList: [],
    actEndTimeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')

    var that = this
    wx.request({
      url: url +'sharkActivity/fetchYzs',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {}
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        var data = e.data
        var pinData =e.data.content
        var timeList = []   
        for (var i = 0; i < pinData.length;i++){
              timeList = timeList.concat({countDown : pinData[i].countDown})
        }
        let endTimeList = [];
        // 将活动的结束时间参数提成一个单独的数组，方便操作
        timeList.forEach(o => { endTimeList.push(o.countDown) })
        that.setData({ actEndTimeList: endTimeList });
        // 执行倒计时函数
        that.countDown();
          that.setData({

            listData: pinData
          })

       
      }
    })
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '0',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
   t = setTimeout(this.countDown, 1000);

  },
  

  goumai:function(e){
    var activityid = e.currentTarget.dataset.activityid
    var shareuser = e.currentTarget.dataset.shareuser
    var wareid = e.currentTarget.dataset.wareid
    wx.navigateTo({
      url: '../index/BeautyKilling/kill_details/kill_details?userIdshaer=' + shareuser + '&wareId=' + wareid + '&activityId=' + activityid,
    })
  },
  goto_yzs:function(e){
    console.log(e.currentTarget.dataset)
    var activityid = e.currentTarget.dataset.activityid
    var shareuser = e.currentTarget.dataset.shareuser
    var wareid = e.currentTarget.dataset.wareid

    wx.navigateTo({
      url: '../index/BeautyKilling/kill_details/collection/collection?userIdshaer=' + shareuser + '&wareid=' + wareid + '&activityid='+activityid,
    })
  },
  goto_xiadan:function(e){
    var activityid = e.currentTarget.dataset.activityid
    var shareuser = e.currentTarget.dataset.shareuser
    var wareid = e.currentTarget.dataset.wareid

    wx.navigateTo({
      url: '../Order/OrderAddress/OrderAddress?wareId=' + wareid + '&activityId=' + activityid + '&tag=' + 2,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // clearTimeout(t)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var wareId = res.target.dataset.wareid
    var activityId = res.target.dataset.activityid
    var userIdshaer = userId
    console.log(userIdshaer)
    console.log(wareId)
    console.log(activityId)

    wx.request({
      url: url + 'sharkActivity/addYzsProcess',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "sysCode": "0902",
        "token": "",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "activityId": activityId,
          "wareId": wareId,
          "payPrice": "0",
          "shareUser": userIdshaer
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        console.log(e)
        
      }
    })
    if (res.from === 'button') {
      return {
        title: '转发',
        path: "pages/index/BeautyKilling/kill_details/collection/collection?wareid=" + wareId + "&activityid=" + activityId + '&userIdshaer=' + userIdshaer,
        success: function (res) {
          console.log('成功', res)

        }
      }
    }
  }
})